<script lang="ts" setup>
import { reactive, ref } from 'vue'
import {ElMessage, FormInstance, FormRules} from 'element-plus'
import {updatePasswordAPI} from '@/apis/user'
import {useUserStore} from '@/stores/user'

const ruleFormRef = ref<FormInstance>()


const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入新密码'))
  } else if(!(value.length>=6&&value.length<=12)) {
    callback(new Error('密码长度在6-12位'))
  } else
   {
    if (ruleForm.checkPass !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass')
    }
    callback()
  }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请确认密码'))
  } else if(!(value.length>=6&&value.length<=12)) {
    callback(new Error('密码长度在6-12位'))
  }else if (value !== ruleForm.pass) {
    callback(new Error("两次输入不一致"))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  pass: '',
  checkPass: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  pass: [{ validator: validatePass, trigger: 'blur' }],
  checkPass: [{ validator: validatePass2, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      updatePassword()
    } else {
      console.log('error submit!')
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const updatePassword = async ()=>{

  if(ruleForm.pass===useUserStore().userInfo.password){
    ElMessage({
      type:'error',
      message:'新密码不能与旧密码相同'
    })
    return
  }

  const res=await updatePasswordAPI(useUserStore().userInfo.uid,ruleForm.pass)
  console.log(res)
  if(res.code===200){
    ElMessage({
      type:'success',
      message:'修改成功'
    })
  }
  useUserStore().userInfo.password=ruleForm.pass
}

</script>

<template>
<div class="panel">
  <div style="width: 400px;display: flex;flex-direction: column;gap: 10px">

    <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
    >
      <el-form-item label="新密码" prop="pass">
        <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
            v-model="ruleForm.checkPass"
            type="password"
            autocomplete="off"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          确定
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</div>
</template>

<style scoped lang="less">
.panel{
  width: 990px;
  height: 600px;
  background-color: white;
  border-radius: 10px;
  padding: 10px 20px;
}
</style>